package com.base.core.action;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.base.core.BaseAction;
import com.base.core.manager.NewsCategoryManager;
import com.base.core.manager.NewsManager;
import com.base.core.model.News;
import com.base.core.model.NewsCategory;
import com.base.core.util.Page;
import com.base.core.util.TaoBaoKeyUtil;

@SuppressWarnings("serial")
public class IndexAction extends BaseAction{
	
	private String indexFlag="";
	
	public String getIndexFlag() {
		return indexFlag;
	}

	public void setIndexFlag(String indexFlag) {
		this.indexFlag = indexFlag;
	}
	
	private List<News> shareList = new ArrayList<News>();
 
	public List<News> getShareList() {
		return shareList;
	}

	public void setShareList(List<News> shareList) {
		this.shareList = shareList;
	}
	
	private List<News> spaceList = new ArrayList<News>();

	public List<News> getSpaceList() {
		return spaceList;
	}

	public void setSpaceList(List<News> spaceList) {
		this.spaceList = spaceList;
	}
	
	private NewsManager newsManager;

	public NewsManager getNewsManager() {
		return newsManager;
	}

	public void setNewsManager(NewsManager newsManager) {
		this.newsManager = newsManager;
	}
	
	private NewsCategoryManager newsCategoryManager;

	public NewsCategoryManager getNewsCategoryManager() {
		return newsCategoryManager;
	}

	public void setNewsCategoryManager(NewsCategoryManager newsCategoryManager) {
		this.newsCategoryManager = newsCategoryManager;
	}

	public String execute(){
		//this.getIpInfo();
		indexFlag = "Y";
		Page page = new Page(5);
		page.setCurrentPageIndex(1);
		NewsCategory category =newsCategoryManager.loadNewsCategory(10000L);
		shareList = newsManager.getNewsList(category,page,null);
		if(shareList!=null && shareList.size()>0){
			for(News ns:shareList){
				ns.setEncodeIdStr(TaoBaoKeyUtil.encode(ns.getId().toString(), "UTF-8"));
			}
		}
		
		category =newsCategoryManager.loadNewsCategory(10001L);
		spaceList = newsManager.getNewsList(category,page,null);
		if(spaceList!=null && spaceList.size()>0){
			for(News ns:spaceList){
				ns.setEncodeIdStr(TaoBaoKeyUtil.encode(ns.getId().toString(), "UTF-8"));
			}
		}
		
		return SUCCESS;
	}
	
	public String tools(){
		return "tools";
	}
	
	public String ipInfo(){
		try{
		this.getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out =this.getResponse().getWriter();
		out.write(this.getIpAddr()+"_split_"+this.getIpLocation());
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
}
